package com.iuie.ns.frame.utils.httpclients;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;

import com.iuie.ns.frame.exception.ServiceException;

public class DefaultHttpResultHandler implements HttpResultHandler<String>{

	@Override
	public String handle(HttpEntity httpEntity) {
		StringBuilder sb = new StringBuilder(12);
		InputStream is = null;
		BufferedReader reader = null;
		try {
			is = httpEntity.getContent();
			reader = new BufferedReader(new InputStreamReader(is));
			
			char[] buf = new char[1024];
			while (reader.read(buf) != -1) {
				sb.append(buf);
			}
		} catch (UnsupportedOperationException e) {
			e.printStackTrace();
			throw new ServiceException("处理结果时异常，UnsupportedOperationException");
		} catch (IOException e) {
			e.printStackTrace();
			throw new ServiceException("处理结果时异常，IOException");
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return sb.toString();
	}

}
